package com.jushu.storbox.util;


import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.SystemClock;

public class PollingAlarmUtils {

	private static final String TAG = "PollingAlarmUtils";
	
	public static void startPollingService(Context context, int seconds,
			int requestCode, String action) {
		LogUtil.i(TAG, "startPollingService");
		AlarmManager manager = (AlarmManager) context
				.getSystemService(Context.ALARM_SERVICE);

		Intent intent = new Intent(action);
		PendingIntent pendingIntent = PendingIntent.getBroadcast(context,
				requestCode, intent, 0);

		//start time
		long triggerAtTime = SystemClock.elapsedRealtime();
		manager.setRepeating(AlarmManager.ELAPSED_REALTIME, triggerAtTime,
				seconds * 1000, pendingIntent);
	}

	// start polling service
	public static void stopPollingService(Context context, int requestCode,
			String action) {
		LogUtil.i(TAG, "stopPollingService");
		AlarmManager manager = (AlarmManager) context
				.getSystemService(Context.ALARM_SERVICE);
		Intent intent = new Intent(action);
		PendingIntent pendingIntent = PendingIntent.getBroadcast(context,
				requestCode, intent, 0);
		// cancel service that executing
		manager.cancel(pendingIntent);
	}
}
